package com.yonyou.crm.cum.customer.ctrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.base.doc.rmitf.IBaseDocRmService;
import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.common.rest.constant.PCCodeConstant;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.cum.customer.entity.CustomerSalesVO;
import com.yonyou.crm.cum.customer.entity.CustomerVO;
import com.yonyou.crm.cum.customer.rmitf.ICustomerRmService;
import com.yonyou.crm.cum.customer.rmitf.ICustomerSalesRmService;
import com.yonyou.crm.sys.modules.entity.MtObjTypeEnum;
import com.yonyou.crm.sys.queryplan.entity.QueryPlanVO;
import com.yonyou.crm.sys.queryplan.rmitf.IQueryPlanRmSerivice;

@RestController
@RequestMapping(value = "/cum/customersales")
public class CustomerSalesCtrl {
	@Autowired
	private ICustomerSalesRmService service;
	
	@Autowired
	private IBaseDocRmService baseDocRmService;
	
	@Autowired
	private ICustomerRmService customerService;
	
	@Autowired
	private IQueryPlanRmSerivice queryRmService;
	
	/**
	 * 获取查询方案、查询条件
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException 
	 */
	@RequestMapping(value = "querytemplate",method = RequestMethod.GET)
	public Object getQueryTemplate(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException{
		JSONObject paramObj = RequestUtil.getParamObject(request);
		List<QueryPlanVO> queryPlanList = new ArrayList<QueryPlanVO>();
		if(paramObj == null){
			throw new CrmMessageException("无查询参数");
		}else{
			Long objId = MtObjTypeEnum.CUSTOMER.getValue();
			Integer clientType = paramObj.getInt("clientType");
//			Integer menuId = paramObj.getInt("menuId");
			Integer menuId = 50;
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("objId", objId);
			param.put("clientType", clientType);
			param.put("menuId", menuId);
			queryPlanList = queryRmService.getQueryPlanList(param);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("1", "type");// 客户类型
		map.put("5", "level");// 客户等级
		map.put("10", "industry");// 行业
		map.put("2", "cannelType");// 渠道类型
		Map<String, Object> baseDocMap = baseDocRmService
				.getDocAndDetailList(map);
		//TODO 添加客户名称、销售公司、省市区
		baseDocMap.put("name", null);
		baseDocMap.put("org", null);
		baseDocMap.put("province_city_district", null);
		JSONObject result = new JSONObject();
		result.put("condition", baseDocMap);
		result.put("plan", queryPlanList);
		return ResponseUtil.getObject(result);
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public Object list(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		int page = paramObject.getInt("page");
		int pageSize = paramObject.getInt("pageSize");
		@SuppressWarnings("unchecked")
		Map<String, Object> searchMap = (Map<String, Object>) paramObject
				.get("searchMap");
		if (searchMap == null) {
			searchMap = new HashMap<String, Object>();
		}
		@SuppressWarnings("unchecked")
		Map<String, Object> searchPlan = (Map<String, Object>) paramObject.get("searchPlan");
		if(searchPlan != null){
			Integer searchPlanId = 0;
			String searchPlanClass = "";
			searchPlanId = (Integer)searchPlan.get("planId");
			searchMap.put("searchPlan", searchPlanId);
			searchPlanClass = (String)searchPlan.get("planClass");
			searchMap.put("searchPlanClass", searchPlanClass);
		}
		
		Page<CustomerVO> customerList = customerService.getList(new Page<CustomerVO>(
				page, pageSize), searchMap);
		return ResponseUtil.getObject(customerList);
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public CustomerSalesVO insert(HttpServletRequest request, HttpServletResponse response){
		CustomerSalesVO customerSale = RequestUtil.getVO(request, CustomerSalesVO.class);
		return service.insert(customerSale);
	}
	
	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	public Object update(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException{
		CustomerSalesVO customerSale = RequestUtil.getVO(request, CustomerSalesVO.class);
		if (customerSale == null) {
        	throw new CrmMessageException("转换实体失败");
		}
		customerSale.setId(id);
		customerSale = service.update(customerSale);
		return ResponseUtil.getObject(customerSale, PCCodeConstant.Success, "保存成功") ;
	}
	
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public Object detail(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		if (id == null) {
        	throw new CrmMessageException("id不能为空");
		}		
		return ResponseUtil.getObject(service.getDetail(id));
	}
	
	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	public Object delete(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		if (id == null) {
        	throw new CrmMessageException("id不能为空");
		}		
		return ResponseUtil.getObject(service.delete(id));
	}
	
	@RequestMapping(value = "allocation", method = RequestMethod.PUT)
	public Object batchAllocation(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String orgIdStr = paramObj.getString("orgIds");
		String cumIdStr = paramObj.getString("cumIds");
		String ownerDeptIdStr = paramObj.getString("ownerDeptId");
		String ownerUserIdStr = paramObj.getString("ownerUserId");
		service.batchAllocation(orgIdStr, cumIdStr, ownerDeptIdStr, ownerUserIdStr);
		return ResponseUtil.getObject("分配成功");
	}

}